package com.springspirit.monitor.cep;

import org.apache.log4j.Logger;

import com.espertech.esper.client.EventBean;
import com.espertech.esper.client.UpdateListener;
import com.springspirit.framework.jms.JmsQueueSender;
import com.springspirit.framework.message.CepMessage;
import com.springspirit.framework.message.LogMessage;

public class LogMessageListener implements UpdateListener {

	Logger logger = Logger.getLogger(LogMessageListener.class);
	
	private JmsQueueSender jmsQueueSender;
	
	public void setJmsQueueSender(JmsQueueSender jmsQueueSender){
		this.jmsQueueSender = jmsQueueSender;
	}

//	public void update(EventBean[] newEvents, EventBean[] oldEvents) {
//
//		EventBean event = newEvents[0];
//
//		if (logger.isDebugEnabled()) {
//			logger.debug("[CEP] event length=" + newEvents.length);
//			logger.debug("[CEP] count=" + event.get("count(errorCategory)"));			
//		}
//		
//		CepMessage cepMessage = new CepMessage(String.valueOf(event.get("errorCategory")));
//		
//		jmsQueueSender.send(cepMessage);
//
//	}
	
	public void update(EventBean[] newEvents, EventBean[] oldEvents) {

		EventBean event = newEvents[0];

		if (logger.isDebugEnabled()) {
			logger.debug("[CEP] event length=" + newEvents.length);
			logger.debug("[CEP] count=" + event.get("count(menu)"));
			logger.debug("[CEP] no=" + event.get("no"));
			logger.debug("[CEP] loginUser=" + event.get("loginUser"));
			logger.debug("[CEP] subSystem=" + event.get("subSystem"));
			logger.debug("[CEP] menu=" + event.get("menu"));
			logger.debug("[CEP] responsePerson=" + event.get("responsePerson"));
			logger.debug("[CEP] layer=" + event.get("layer"));
			logger.debug("[CEP] errorCategory=" + event.get("errorCategory"));
			logger.debug("[CEP] errorMessage=" + event.get("errorMessage"));
			logger.debug("[CEP] loginIpAddress=" + event.get("loginIpAddress"));
			logger.debug("[CEP] timeStamp=" + event.get("timeStamp"));
		}
		
		CepMessage cepMessage = new CepMessage(
				String.valueOf(event.get("count(menu)")), 
				String.valueOf(event.get("errorCategory")), 
				String.valueOf(event.get("timeStamp")));
		
		jmsQueueSender.send(cepMessage);

	}

}
